package com.pet.exchange;

import com.pet.mapper.*;
import com.pet.service.impl.InvitationServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Component
@Slf4j
public class DeleteUtil {
    @Autowired//操作用户表
    private UserMapper userMapper;
    @Autowired//操作帖子表
    private InvitationMapper invitationMapper;
    @Autowired//操作领养申请表
    private PetAdoptApplicationMapper petAdoptApplicationMapper;
    @Autowired//操作领养表
    private PetAdoptMapper petAdoptMapper;
    @Autowired//操作健康表
    private PetHealthInformationMapper petHealthInformationMapper;
    @Autowired//操作宠物表
    private PetInformationMapper petInformationMapper;
    @Autowired//操作求助表
    private PetHelpInformationMapper petHelpInformationMapper;

    public void deleteInvitation(Integer id) {

    }

    /**
     * 管理员
     * 删除用户信息 以该用户的id为外键删除对应的帖子、收藏、点赞、评论、送养、领养、领养申请、宠物信息
     * @param userId
     */
    @Transactional
    public void deleteUserInformation(Integer userId) {
        try {
            /*
            根据用户id删除相关的帖子、收藏、点赞、评论、送养、领养、领养申请、宠物信息
             */

            //1、删除用户信息
            userMapper.deleteUserInformation((userId));
            //2、删除帖子信息表 同时删除相关的评论、点赞、收藏
            try {
                //根据userId删除相关的评论、点赞、收藏
                invitationMapper.deleteInvitationCollectByUserId(userId);
                invitationMapper.deleteInvitationLikeByUserId(userId);
                invitationMapper.deleteInvitationRemarksByUserId(userId);
                //删除帖子信息表
                invitationMapper.deleteInvitationByUserId(userId);
            }catch (Exception exception){
                log.error(exception.getMessage());
                throw new RuntimeException("删除帖子失败");
            }
            //3、删除领养申请信息表
            petAdoptApplicationMapper.deletePetAdoptApplicationByUserId(userId);
            //4、删除领养信息表
            petAdoptMapper.deletePetAdoptByUserId(userId);
            //5、删除宠物信息表
            petInformationMapper.deletePetInformationByUserId(userId);
            //6、删除健康信息表
            petHealthInformationMapper.deletePetHealthInformationByUserId(userId);
            //7、删除求助信息
            petHelpInformationMapper.deletePetHelpInformationByUserId(userId);


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
